package com.ship.dispatch.config;

import com.ship.dispatch.bpm.result.ProcessInstanceListener;
import lombok.AllArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;

import org.flowable.spring.SpringProcessEngineConfiguration;

import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

@AllArgsConstructor
@Configuration
public class BpmConfiguration implements ApplicationListener<ContextRefreshedEvent> {
    private final SpringProcessEngineConfiguration configuration;
    @Lazy
    @Resource
    private ProcessInstanceListener processInstanceListener;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        dispatcher.addEventListener(processInstanceListener, FlowableEngineEventType.PROCESS_COMPLETED);
    }
}
